import time
from base_camera import BaseCamera
import glob
import cv2

class Camera(BaseCamera):
    """An emulated camera implementation that streams a repeated sequence of
    files 1.jpg, 2.jpg and 3.jpg at a rate of one frame per second."""
    # imgs = [open(f + '.jpg', 'rb').read() for f in ['1', '2', '3']]
    file_list = glob.glob('/home/cc/WORK/ws_matlab/VOC2012_eval/my_input/kittivoc_ori_1_1/kittivoc_test/JPEGImages/*.jpg')
    # images = (open(f, 'rb').read() for f in file_list)
    images = (cv2.imread(f) for f in file_list)

    @staticmethod
    def frames():
        while True:
            time.sleep(1)
            # yield Camera.imgs[int(time.time()) % 3]
            img = Camera.images.__next__()
            # ------process start-------------
            # example
            start_time = time.time()  # start time
            img_out = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            # img_out = cv2.Canny(gray, 50, 150)
            # fps = str(1.0 / (time.time() - start_time))  # calculate the fps
            # cv2.putText(img_out, "FPS: {0}".format(fps), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 255), 1)
            ms = str( "%.2f" % ((time.time() - start_time) * 1000.0) )  # calculate the ms
            img_out = cv2.cvtColor(img_out, cv2.COLOR_GRAY2BGR) # converted to a three-channel array
            cv2.rectangle(img_out, (10, 10), (100, 30), (255, 0, 0), -1)
            cv2.putText(img_out, "MS: {0}".format(ms), (15, 25), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
            # example end
            # ------process end---------------
            # yield img
            ret, jpeg = cv2.imencode('.jpg', img_out)
            yield jpeg.tobytes()
